<?php
// +----------------------------------------------------------------------
// | Author: 黄泽宏 <2623213969@qq.com>
// +----------------------------------------------------------------------

namespace app\home\controller;
/**
 * 我的钱包控制器
 */
class WalletController extends HomeController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 我的钱包页面
     */
    public function index()
    {
        $price = db('user')->where(array("id" => $this->user_id))->field("money")->find();
        $this->assign("price", $price['money']);
        return $this->fetch('index');
    }

    /**
     * 我的钱包详情页面
     */
    public function wallet_detail()
    {
        $user_account = db('user_account_log')->where(array('user_id' => $this->user_id))->order("id desc")->select();
        $this->assign("user_account", $user_account);
        return $this->fetch('wallet_detail');
    }

    public function wallet_rechange(){
        $recharge = db('System')->where(['id'=>1])->field('recharge_give,recharge_give_number,min_recharge')->find();
        $recharge['goods_name'] = db('Goods')->where(['id'=>$recharge['recharge_give']])->value('goods_name');
        if(IS_POST){
            $money = input('param.money');
//            if($money < $recharge['min_recharge']){
//                $this->error('最低充值金额为'.$recharge['min_recharge']);
//            }
            $url ='http://'.$_SERVER['HTTP_HOST'].'/index.php/Home/Wxpay/recharge?money='.$money;
            header("Location: $url");
        }
        $this->assign([
            'recharge'=>$recharge,
        ]);
        return $this->fetch('wallet_rechange');
    }


    /**
     * 提现页面
     */
    public function wallet_withdraw()
    {
        if (IS_POST) {
            $data = input('param.');
            $user_list = db('user')->where(array('id' => $this->user_id))->find();
            if ($user_list['integral'] < $data['integral']) {
                $this->error("积分不足！");
            }
            if (!preg_match("/^\d*$/", $data['integral'])) {
                $this->error("纯数字,不含中文！");
            }
            $system = db('system')->find();
            $price = $data['integral'] / $system['point_rate'];
            $withdraw_data = array(
                'user_id' => $this->user_id,
                'nickname' => $user_list['nickname'],
                'integral' => $data['integral'],
                'money' => $price,
                'addtime' => time(),
                'status' => 1
            );
            $res = db('wallet_withdraw')->save($withdraw_data);
            if ($res) {
                db('user')->where(array('id' => $this->user_id))->update(array('integral' => array('exp', 'integral-' . $data['integral'])));
                $this->success("提现成功,待审核!");
            }
        } else {
            $system = db('system')->find();
            $this->assign('system', $system);
            return $this->fetch('wallet_withdraw');
        }


    }


}